package com.hjjt.cameralib.utils

import android.graphics.Bitmap
import android.graphics.Matrix


/**
 *
 * @ProjectName:    CameraLib
 * @Package:        com.hjjt.cameralib.utils
 * @ClassName:      BMPUtil
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/6/6 10:18
 */
object BMPUtil {
    /**
     * 将图片按照某个角度进行旋转
     *
     * @param bm
     * 需要旋转的图片
     * @param degree
     * 旋转角度
     * @return 旋转后的图片
     */
    fun rotateBitmapByDegree(bm: Bitmap, degree: Int): Bitmap? {
        var returnBm: Bitmap? = null

        // 根据旋转角度，生成旋转矩阵
        val matrix = Matrix()
        matrix.postRotate(degree.toFloat())
        matrix.postScale(-1F,1F)
        try {
            // 将原始图片按照旋转矩阵进行旋转，并得到新的图片
            returnBm = Bitmap.createBitmap(bm, 0, 0, bm.width, bm.height, matrix, true)
        } catch (e: OutOfMemoryError) {
        }
        if (returnBm == null) {
            returnBm = bm
        }
        if (bm != returnBm) {
            bm.recycle()
        }
        return returnBm
    }
}